package com.upu173.music.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.andbase.library.app.base.AbBaseFragment;
import com.andbase.library.utils.AbStrUtil;
import com.andbase.library.view.recycler.AbRecyclerView;
import com.andbase.library.view.recycler.AbSpaceItemVerticalDecoration;
import com.upu173.music.R;
import com.upu173.music.activity.MusicBaseActivity;
import com.upu173.music.adapter.SongRecyclerViewAdapter;
import com.upu173.music.model.Song;
import com.upu173.music.utils.MusicUtil;

import java.util.ArrayList;
import java.util.List;


public class SongListFragment extends AbBaseFragment {

    private MusicBaseActivity activity;
    public List<Song> items = null;
    private SongRecyclerViewAdapter recyclerViewAdapter = null;
    private AbRecyclerView recyclerView = null;
    private String folderPath = null;
    private View rootView = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.music_fragment_song_list, container, false);

        activity = (MusicBaseActivity)this.getActivity();

        items  = new ArrayList<>();

        recyclerViewAdapter = new SongRecyclerViewAdapter(activity,items);

        recyclerView = (AbRecyclerView)rootView.findViewById(R.id.recycler_list);

        recyclerView.addItemDecoration(new AbSpaceItemVerticalDecoration(activity,1));

        recyclerView.setRecyclerViewAdapter(recyclerViewAdapter);

        folderPath = getArguments().getString("folderPath");

        if(!AbStrUtil.isEmpty(folderPath)){
            List<Song> songList =  MusicUtil.getSongListFromFolder(activity,folderPath);
            if(songList!=null && songList.size() > 0){
                hideEmptyView(rootView);
                items.addAll(songList);
                recyclerViewAdapter.notifyDataSetChanged();
            }else{
                showEmptyView(rootView,R.layout.music_view_no_song_list);
            }

        }else{
            loadData();
        }

        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public void loadData(){
        items.clear();
        List<Song> songList = MusicUtil.getSongList(activity);
        if(songList!=null && songList.size() > 0){
            items.addAll(songList);
        }
        recyclerViewAdapter.notifyDataSetChanged();
    }
}
